package com.amoyt.project.controller.admin;


import com.amoyt.project.annotations.BusinessLog;
import com.amoyt.project.annotations.ModuleLog;
import com.amoyt.project.domain.dto.insert.ProjectApprovalAddDto;
import com.amoyt.project.domain.dto.query.ProjectPageQuery;
import com.amoyt.project.domain.dto.update.ProjectApplyUpdateDto;
import com.amoyt.project.domain.dto.update.ProjectApprovalUpdateDto;
import com.amoyt.project.domain.res.Result;
import com.amoyt.project.exception.BusinessException;
import com.amoyt.project.exception.enums.BusinessExceptionEnum;
import com.amoyt.project.service.admin.ProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 项目审批主表（存储申请信息） 前端控制器
 * </p>
 *
 * @author AmoyT
 * @since 2025-08-24
 */
@RestController
@RequestMapping("/admin/project")
@Tag(name = "项目接口",description = "项目审批表（存储申请信息）接口")
@ModuleLog(module = "项目操作模块")
@Slf4j
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    /**
     * 查询自己能查到的所有项目，项目列表
     * @param query
     * @return
     */
    @Operation(summary = "项目列表",method = "POST")
    @PostMapping("/list-child")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result listAll(@RequestBody ProjectPageQuery query){
        if (query == null || query.getPage() == null || query.getPageSize() == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return projectService.listAll(query);
    }

    /**
     * 当前账号申请列表
     * @param query
     * @return
     */
    @Operation(summary = "当前账号申请列表",method = "POST")
    @PostMapping("/list-curr")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result listCurrApprovalList(@RequestBody ProjectPageQuery query) {
        if (query ==  null || query.getPage() == null || query.getPageSize() == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return projectService.listCurrApprovalList(query);
    }

    /**
     * 当前账号-需要审批的项目列表或者已经审批的列表,下级列表即可
     * @param query
     * @return
     */
    @Operation(summary = "当前账号-项目审批列表",method = "POST")
    @PostMapping("/list-approval")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result listApprovalList(@RequestBody ProjectPageQuery query) {
        if (query == null || query.getPage() == null || query.getPageSize() == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return projectService.listApprovalList(query);
    }

    /**
     * 项目审批-操作
     * @param dto
     * @return
     */
    @Operation(summary = "项目审批操作",method = "PUT")
    @PutMapping("/approval")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "项目审批操作")
    public Result approval(@RequestBody ProjectApprovalUpdateDto dto) {
        log.info("项目审批操作,dto={}",dto);
        if (dto == null || dto.getProjectId() == null || dto.getProjectId().equals("")){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return projectService.approvalUpdate(dto);
    }

    /**
     * 项目申请
     * @param dto
     * @return
     */
    @Operation(summary = "项目申请",method = "POST")
    @PostMapping("/apply")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "项目申请")
    public Result apply(@RequestBody ProjectApprovalAddDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getProjectName()) || StringUtils.isBlank(dto.getProjectLevel())){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return projectService.doApply(dto);
    }

    /**
     * 更新项目申请
     * @param dto
     * @return
     */
    @Operation(summary = "更新项目申请",method = "PUT")
    @PutMapping("/update-apply")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "更新项目申请")
    public Result updateApply(@RequestBody ProjectApplyUpdateDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getProjectId())){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return projectService.updateApply(dto);
    }

    /**
     * 可申请项目级别
     * @return
     */
    @Operation(summary = "可申请项目级别",method = "GET")
    @GetMapping("/list-can-apply")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result queryProjectLevel() {
        return projectService.queryProjectLevel();
    }


    @Operation(summary = "批量删除",method = "DELETE")
    @DeleteMapping("/delete/{ids}")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "批量删除项目")
    public Result delete(@PathVariable List<String> ids) {
        if (ids == null || ids.isEmpty()){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return projectService.delete(ids);
    }

    /**
     * 项目详情 包括审批进度
     * @param projectId
     * @return
     */
    @Operation(summary = "项目详情",method = "GET")
    @GetMapping("/detail/{projectId}")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result queryProjectDetail(@PathVariable String projectId) {
        if (StringUtils.isBlank(projectId)){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return projectService.queryProjectDetail(projectId);
    }

    /**
     * 项目详情
     * @param projectId
     * @return
     */
    @Operation(summary = "审批详情列表（可能废弃）",method = "GET")
    @GetMapping("/approval-detail/{projectId}")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result queryProjectApprovalDetail(@PathVariable String projectId) {
        if (StringUtils.isBlank(projectId)){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return projectService.queryProjectApprovalDetail(projectId);
    }

    /**
     * 近7日数据
     * @return
     */
    @Operation(summary = "近7日数据",method = "GET")
    @GetMapping("/trend")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result queryProjectTrend() {
        return projectService.queryProjectTrend();
    }
}

